package com.canbot.u05.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class NetWorkChangeBroadcastReceiver extends BroadcastReceiver {

        public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

        @Override
        public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(CONNECTIVITY_CHANGE_ACTION)) {
                        ConnectivityManager connectivityManager =
                                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                        if (connectivityManager != null) {
                                NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
                                for (int i = 0; i < networkInfos.length; i++) {
                                        State state = networkInfos[i].getState();
                                        if (NetworkInfo.State.CONNECTED == state) {
                                                System.out.println("------------> Network is ok");
                                                return;
                                        }
                                }
                        }

                }

                //没有执行return,则说明当前无网络连接
                System.out.println("------------> Network is validate");
//        intent.setClass(context, NetWorkErrorActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//        context.startActivity(intent);
        }

}
